﻿/*
 * 用户密码，报价密码，确认状态管理。
 * 
 * 创建标识：周司徒
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Common;
using System.Text;

public partial class Manage_UserPasswordManage : System.Web.UI.Page, IAccess
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CurrentAccount.CheckSessionTimeOut(this);
        if (!IsPostBack)
        {
            BindData();
        }
    }

    private void BindData()
    {
        int totalrecord;
        var list = BLL_Factory.IBLL_Manage.GetCompany_Project(new { ProjectId = CurrentAccount.ProjectId, SortFields = "CompanyId", UserCode = txtCompanyCode.Text, CompanyName = txtCompanyName.Text }, pager1.CurrentPage, pager1.PageSize, out totalrecord);

        pager1.RecordCount = totalrecord;
        gvcompany.DataSource = list;
        gvcompany.DataBind();

    }
    protected void btnResetPassword_Click(object sender, EventArgs e)
    {
        var list = GetSelectedCompanyId();
        if (list.Count > 0)
        {
            BLL_Factory.IBLL_Manage.ResetPassword(list, CurrentAccount.ProjectId);

            log4netManager.LogInfo("初始化用户密码", string.Format("初始化用户密码成功，操作人：{0}，操作IP：{1}，操作信息：{2}。", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, GetString(list)), null);

            HZ.Web.WebUtil.Alert("密码已初始化为：123456", this);
        }
        else
        {
            HZ.Web.MessageBox.Show("没有选择任何企业");
        }
    }
    protected void btnClearBidPassword_Click(object sender, EventArgs e)
    {

        if (CurrentProject.InBidTime > 0)
        {

            HZ.Web.MessageBox.Show("报价时间已过,不允许清空报价密码！");
            return;
        }
        else
        {
            var companylist = GetSelectedCompanyId();

            foreach (var item in companylist) // 逼企业强制重新登录
            {
                if (HZ.BidSystem.BLL.BLL_Cache.ForbidLogin.ContainsKey(item))
                {
                    HZ.BidSystem.BLL.BLL_Cache.ForbidLogin[item] = DateTime.Now;
                }
                else
                {
                    HZ.BidSystem.BLL.BLL_Cache.ForbidLogin.Add(item, DateTime.Now);
                }

            }
            // 报价时间到已过  不允许重置密码

            if (companylist.Count > 0)
            {
                BLL_Factory.IBLL_Manage.ClearBidPassword(companylist, CurrentAccount.ProjectId);
                log4netManager.LogInfo("清空报价密码", string.Format("清空报价密码成功，操作人：{0}，操作IP：{1}，操作信息：{2}。", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, GetString(companylist)), null);

                HZ.Web.WebUtil.Alert("清空报价密码成功", this);
            }
            else
            {
                HZ.Web.MessageBox.Show("没有选择任何企业");
            }
        }
    }
    protected void btnClearComfirmStatus_Click(object sender, EventArgs e)
    {
        var companylist = GetSelectedCompanyId();
        if (companylist.Count > 0)
        {
            BLL_Factory.IBLL_Manage.ClearComfirmStatus(companylist, CurrentAccount.ProjectId);
            log4netManager.LogInfo("清除确认报价", string.Format("清除确认报价成功，操作人：{0}，操作IP：{1}，操作信息：{2}。", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, GetString(companylist)), null);
            HZ.Web.WebUtil.Alert("清除确认报价状态成功", this);
        }
        else
        {
            HZ.Web.MessageBox.Show("没有选择任何企业");
        }

    }

    /// <summary>
    /// 获取被选中的企业列表
    /// </summary>
    /// <returns></returns>
    protected IList<Int32> GetSelectedCompanyId()
    {
        if (IsPostBack)
        {
            var list = new List<Int32>();
            foreach (GridViewRow item in gvcompany.Rows)
            {
                if (item.RowType == DataControlRowType.DataRow)
                {
                    var checkbox = item.FindControl("chkselect") as CheckBox;
                    if (checkbox.Checked)
                    {
                        list.Add(int.Parse(item.Cells[0].Text));
                    }
                }
            }
            return list;
        }
        else
        {

            throw new Exception("调用错误");
        }
    }


    protected void pager1_PageChanging(object sender, HZ.Web.PageChaningEventArgs e)
    {
        BindData();
    }
    protected void btnClearComfirmStatusAll_Click(object sender, EventArgs e)
    {
        BLL_Factory.IBLL_Manage.ClearComfirmStatusAll(CurrentAccount.ProjectId);
        log4netManager.LogInfo("清除所有企业确认状态", string.Format("清除所有企业确认状态成功，操作人：{0}，操作IP：{1}。", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP), null);
    }
    protected void btnsearch_Click(object sender, EventArgs e)
    {
        pager1.CurrentPage = 1;
        BindData();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        BLL_Factory.IBLL_Manage.ClearDecStatusAll(CurrentAccount.ProjectId);
        log4netManager.LogInfo("清除所有企业解密状态", string.Format("清除所有企业解密状态成功，操作人：{0}，操作IP：{1}。", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP), null);

    }

    #region IAccess Members

    IList<EnumAccess> IAccess.AccessList
    {
        get
        {
            var list = new List<EnumAccess>();
            list.Add(EnumAccess.Manage);
            return list;
        }

    }

    #endregion

    private static string GetString(IList<int> list)
    {
        StringBuilder sb = new StringBuilder();
        bool first = true;
        foreach (var item in list)
        {
            if (first)
            {
                sb.AppendFormat("{0},", item);
                first = false;
            }
            else
            {
                sb.AppendFormat("{0}.", item);

            }
        }
        return sb.ToString();
    }
    /// <summary>
    /// 报价信息公示
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            int i = BLL_Factory.IBLL_Manage.CompanyMessageGongshi();
            log4netManager.LogInfo("报价信息公示", string.Format("报价信息公示成功，操作人：{0}，操作IP：{1}。", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP), null);
            if (i > 0)
            {
                HZ.Web.WebUtil.Alert("企业解密报价信息公示成功", this);
            }
            else
            {
                HZ.Web.WebUtil.Alert("企业解密报价信息公示失败", this);
            }
        }
        catch (Exception ex)
        {
            HZ.Web.WebUtil.Alert("企业解密报价信息公示成功", this);
        }

    }
}
